#include "stdafx.h"
#include "dataStruct.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


areaInfo& areaInfo::operator =(const areaInfo& i)
{
	if(this == &i)
		return *this;
	memcpy(areaName,i.areaName,50);
	desktops = i.desktops;
	return *this;
}

int areaInfo::save(void *buffer) const 
{
	int factSize = 0;
	MAC_WRITEBUFFER(buffer,areaName,50 * sizeof(wchar_t),factSize);
	int nCount = (int)desktops.size();
	MAC_WRITEVAR(buffer,int,nCount,factSize);
	for (int i = 0; i < nCount; i++)
	{
		MAC_WRITEBUFFER(buffer,&desktops[i],sizeof(desktopInfo),factSize);
	}
	return factSize;
}

int areaInfo::load(void* buffer)
{
	int factSize = 0;
	MAC_READBUFFER(buffer,areaName,50 * sizeof(wchar_t),factSize);
	int nCount = 0;
	MAC_READVAR(buffer,int,nCount,factSize);
	desktops.resize(nCount);
	//(int)desktops.size();
	for (int i = 0; i < nCount; i++)
	{
		MAC_READBUFFER(buffer,&desktops[i],sizeof(desktopInfo),factSize);
	}
	return factSize;
}

int timeInterval::save(void *buffer) const
{
	int factSize = 0;
	factSize += startTime.save(buffer);
	if (buffer)
		buffer = (char*)buffer + factSize;

	factSize += endTime.save(buffer);
	return factSize;
}

int timeInterval::load(void* buffer)
{
	int factSize = 0;
	factSize += startTime.load(buffer);
	buffer = (char*)buffer + factSize;

	factSize += endTime.load(buffer);
	return factSize;
}

bool timeInterval::isBetweenTime(const hjcTime& testTime)const
{
	if (testTime.getTotalSecond() <= endTime.getTotalSecond() && testTime.getTotalSecond() >= startTime.getTotalSecond())
	{
		return true;
	}
	return false;
}

int hjcTime::save(void *buffer) const
{
	int factSize = 0;
	MAC_WRITEVAR(buffer,unsigned char,hour,factSize);
	MAC_WRITEVAR(buffer,unsigned char,minute,factSize);
	MAC_WRITEVAR(buffer,unsigned char,second,factSize);
	return factSize;
}

int hjcTime::load(void* buffer)
{
	int factSize = 0;
	MAC_READVAR(buffer,unsigned char,hour,factSize);
	MAC_READVAR(buffer,unsigned char,minute,factSize);
	MAC_READVAR(buffer,unsigned char,second,factSize);
	return factSize;
}

int bookSetData::save(void* buffer)const
{
	int factSize = 0;
	MAC_WRITEVAR(buffer,UINT,m_preserveDay,factSize);
	MAC_WRITEVAR(buffer,UINT,m_wait,factSize);
	MAC_WRITEVAR(buffer,UINT,m_change,factSize);

	int nCount = (int)m_timeInterval.size();
	MAC_WRITEVAR(buffer,int,nCount,factSize);
	for (int i = 0; i < nCount; i++)
	{
		int nSize = m_timeInterval[i].save(buffer);
		factSize += nSize;
		if (buffer)
			buffer = (char*)buffer + nSize;
	}

	nCount = (int)m_vMore.size();
	MAC_WRITEVAR(buffer,int,nCount,factSize);
	for (int i = 0; i < nCount; i++)
	{
		MAC_WRITEBUFFER(buffer,m_vMore[i], 50 * sizeof(wchar_t),factSize);
	}
	return factSize;

}

int bookSetData::load(void* buffer)
{
	int factSize = 0;
	MAC_READVAR(buffer,UINT,m_preserveDay,factSize);
	MAC_READVAR(buffer,UINT,m_wait,factSize);
	MAC_READVAR(buffer,UINT,m_change,factSize);

	int nCount = 0;
	MAC_READVAR(buffer,int,nCount,factSize);
	m_timeInterval.resize(nCount);
	for (int i = 0; i < nCount; i++)
	{
		int nSize = m_timeInterval[i].load(buffer);
		factSize += nSize;
		buffer = (char*)buffer + nSize;
	}

	nCount = 0;
	MAC_READVAR(buffer,int,nCount,factSize);
	m_vMore.resize(nCount);
	for (int i = 0; i < nCount; i++)
	{
		m_vMore[i] = new wchar_t[50];
		MAC_READBUFFER(buffer,m_vMore[i], 50 * sizeof(wchar_t),factSize);
	}
	return factSize;
}

bookSetData::~bookSetData()
{
	int nSize = (int)m_vMore.size();
	for (int i = 0; i < nSize; i++)
	{
		wchar_t* str = m_vMore[i];
		delete []str;
		m_vMore[i] = NULL;
	}
}

void bookSetData::addMoreString(const wchar_t* str,int length /* = 50*/)
{
	length = length > 50 ?50 : length;
	wchar_t* newStr = new wchar_t[50];
	memset(newStr,0,50 * sizeof(wchar_t));
	memcpy(newStr,str,sizeof(wchar_t)*length);
	m_vMore.push_back(newStr);
}

void bookSetData::delMoreString(int i)
{
	vector<wchar_t*>::iterator it = m_vMore.begin();
	wchar_t* delStr = m_vMore[i];
	delete []delStr;
	m_vMore.erase(it + i);
}

void bookSetData::clearMoreString()
{
	for (size_t i = 0; i < m_vMore.size(); i++)
	{
		wchar_t* delStr = m_vMore[i];
		delete []delStr;
	}
	m_vMore.clear();
}
	void clearMoreString();
int Order::save(void* buffer)const
{
	int nSize = 0;
	MAC_WRITEVAR(buffer,INT64,nSerial,nSize);
	MAC_WRITEBUFFER(buffer,areaName,50 * sizeof(wchar_t),nSize);
	MAC_WRITEBUFFER(buffer,orderEmployeeID,50 * sizeof(wchar_t),nSize);
	MAC_WRITEVAR(buffer,hjcDateTime,time,nSize);
	MAC_WRITEBUFFER(buffer,deskName,50 * sizeof(wchar_t),nSize);
	MAC_WRITEVAR(buffer,double,fSeverFee,nSize);
	MAC_WRITEBUFFER(buffer,vipId,50 * sizeof(wchar_t),nSize);
	MAC_WRITEBUFFER(buffer,casherId,50 * sizeof(wchar_t),nSize);
	int nCount = (int)vMainOrder.size();
	MAC_WRITEVAR(buffer,int,nCount,nSize);
	for (int i = 0; i < nCount; i++)
	{
		MAC_WRITEVAR(buffer,OrderMain,vMainOrder[i],nSize);
	}
	return nSize;
}

int Order::load(void* buffer)
{
	int nSize = 0;
	MAC_READVAR(buffer,INT64,nSerial,nSize);
	MAC_READBUFFER(buffer,areaName,50 * sizeof(wchar_t),nSize);
	MAC_READBUFFER(buffer,orderEmployeeID,50 * sizeof(wchar_t),nSize);
	MAC_READVAR(buffer,hjcDateTime,time,nSize);
	MAC_READBUFFER(buffer,deskName,50 * sizeof(wchar_t),nSize);
	MAC_READVAR(buffer,double,fSeverFee,nSize);
	MAC_READBUFFER(buffer,vipId,50 * sizeof(wchar_t),nSize);
	MAC_READBUFFER(buffer,casherId,50 * sizeof(wchar_t),nSize);
	int nCount = 0;
	MAC_READVAR(buffer,int,nCount,nSize);
	vMainOrder.resize(nCount);
	for (int i = 0; i < nCount; i++)
	{
		MAC_READVAR(buffer,OrderMain,vMainOrder[i],nSize);
	}
	return nSize;
}

Order::Order()
{
	memset(areaName,0,50 * sizeof(wchar_t));
	memset(orderEmployeeID,0,50 * sizeof(wchar_t));
	memset(deskName,0,50 * sizeof(wchar_t));
	memset(vipId,0,50 * sizeof(wchar_t));
	memset(casherId,0,50 * sizeof(wchar_t));
	fSeverFee = 0;

}


int discountPlan::save(void* buffer)const
{
	int nSize = 0;
	MAC_WRITEBUFFER(buffer,discountName,50 * sizeof(wchar_t),nSize);
	int nCount = (int)vDiscount.size();
	MAC_WRITEVAR(buffer,int,nCount,nSize);
	for (int i = 0; i < nCount; i++)
	{
		const discount& dis = vDiscount[i];
		MAC_WRITEVAR(buffer,discount,dis,nSize);
	}
	return nSize;
}

int discountPlan::load(void* buffer)
{
	int nSize = 0;
	MAC_READBUFFER(buffer,discountName,50 * sizeof(wchar_t),nSize);
	int nCount = 0;
	MAC_READVAR(buffer,int,nCount,nSize);
	vDiscount.resize(nCount);
	for (int i = 0; i < nCount; i++)
	{
		discount& dis = vDiscount[i];
		MAC_READVAR(buffer,discount,dis,nSize);
	}
	return nSize;
}

void Order::combineOrder(const Order& srcOrder)
{
	for (size_t i = 0; i < srcOrder.vMainOrder.size(); i++)
	{
		vMainOrder.push_back(srcOrder.vMainOrder[i]);
	}
}